<%--
  Created by IntelliJ IDEA.
  User: ASUS
  Date: 2018/7/8
  Time: 15:53
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
    <jsp:include page="/static/common/easyui_header.jsp"></jsp:include>

    <script>
        function formatterCollege(value, row, index) {
            return value ? value.name : "";
        }
        function formatterCampus(value, row, index) {
            return value ? value.name : "";
        }
        function formatterClassroom(value, row, index) {
            return value ? value.name : "";
        }
        function formatterEmployee(value, row, index) {
            return value ? value.realName: "";
        }

        function formatterClassStatus(value, row, index) {
            //获取课程时间
            var beginTime=row.classBeginTime;
            beginTime = beginTime.substring(0,19);
            beginTime = beginTime.replace(/-/g,'/');
            var endTime=row.classEndTime;
            endTime = endTime.substring(0,19);
            endTime = endTime.replace(/-/g,'/');
            //开始时间
            var classBeginTime = new Date(beginTime).getTime();
            //结束时间
            var  classEndTime= new Date(endTime).getTime();
            //获取现在的时间
            var newDate=new Date();
            if(newDate.getTime()>classBeginTime&&newDate.getTime()<classEndTime){
                return "<span style='color:green'>已开班</span>";
            }else if(newDate.getTime()<classBeginTime){
                return "<span style='color:red'>未开班</span>";
            }else{
                return "<span style='color:gray'>已毕业</span>";
                //毕业后设置教室状态为停用根据索引查出该班级信息,获取该班级所拥有的教室id/修改教室状态
                $.post("/classroom/diasableClassroom",{"id":index})
            }
        }
        $(function () {


            //弹窗渲染  classgrade_add_linkbuttons
            $("#classgrade_add_dialog").dialog({
                title: "添加",
                buttons: '#classgrade_add_linkbuttons',
                closed: true

            });

            $('#classgrade_daragrid').datagrid({
                url: '/classgrade/list',
                fit: true,
                fitColumns: true,
                border: false,
                rownumbers: true,
                pagination: true,
                singleSelect: true,
                toolbar: "#classgrade_linkbuttons",
                columns: [[
                    {field: 'name', title: '班级名称', width: 100},
                    {field: 'stuNumber', title: '学生数量', width: 100},
                    {field: 'classBeginTime', title: '开班时间', width: 100},
                    {field: 'classEndTime', title: '结课时间', width: 100},
                    {field: 'campus', title: '校区', width: 100, formatter: formatterCampus},
                    {field: 'college', title: '学院', width: 100, formatter: formatterCollege},
                    {field: 'classStatus', title: '状态', width: 100,formatter: formatterClassStatus},
                    {field: 'classroom', title: '所用教室', width: 100, formatter: formatterClassroom},
                    {field: 'employee', title: '班主任', width: 100, formatter: formatterEmployee},
                ]]
            });
            var cmdObj = {
                //添加班级
                addClass: function () {
                    //清除表单数据
                    $("#classgrade_add_form").form("clear");
                    //设置标题
                    $("#classgrade_add_dialog").dialog("setTitle", "添加班级");
                    //打开弹窗
                    $("#classgrade_add_dialog").dialog("open");
                },
                reloadClass:function () {
                    $("#classgrade_daragrid").datagrid("reload")
                },

                //添加班级和编辑班级上的确定按钮
                save: function () {
                    var url="/classgrade/save";
                    //获取选中行id
                    var classId=$("#class_id").val();
                    console.log(classId);
                    if(classId){
                        url="/classgrade/update";
                    }
                    $("#classgrade_add_form").form('submit',{
                        url: url,
                        success: function (data) {
                           data=$.parseJSON(data)
                            if(data.success){

                                $.messager.alert("提示","保存成功","info",function () {
                                   //关闭窗口
                                    cmdObj.cancel();
                                    //刷新页面
                                    cmdObj.reloadClass();
                                })
                            }else{
                                $.messager.alert("提示",data.msg,"error");
                            }
                        }
                    });

                },
                //编辑编辑管理数据
                editClass:function () {
                    //获取选中行
                    var selectedRow=$("#classgrade_daragrid").datagrid("getSelected")
                    if(!selectedRow){
                        $.messager.alert("提示","请选中一行数据进行操作","error")
                        return;
                    }
                    //对关联对象的回显

                    if(selectedRow.campus){
                        selectedRow['campus.id']=selectedRow.campus.id;
                    }
                    if(selectedRow.college){
                        selectedRow['college.id']=selectedRow.college.id;
                    }
                    if(selectedRow.employee){
                        selectedRow['employee.id']=selectedRow.employee.id;
                    }
                    if(selectedRow.classroom){
                        selectedRow['classroom.id']=selectedRow.classroom.id;
                    }
                    //读取选中行的数据进行回填
                    $("#classgrade_add_form").form("load",selectedRow);
                    //设置标题
                    $("#classgrade_add_dialog").dialog("setTitle", "编辑班级");
                    //打开弹窗
                    $("#classgrade_add_dialog").dialog("open");

                },
                //删除功能
                removeClass:function () {
                        $.messager.alert("提示","给看不给删","error");
                },
                //关闭dialog
                cancel:function () {
                    $("#classgrade_add_dialog").dialog("close");
                },
                //分配班主任
                allotTeacher:function () {
                    //获取选中行
                    var selectedRow=$("#classgrade_daragrid").datagrid("getSelected")
                    if(!selectedRow){
                        $.messager.alert("提示","请选中一行数据进行操作","error")
                        return;
                    }
                }
            }
            //按钮点击事件
            $("a[data-cmd]").click(function () {
                var cmd = $(this).data("cmd");
                //cmdObj[cmd]  :根据cmd的值,找到 cmdObj 对应的方法
                //cmdObj[cmd]() : () ,触发方法的执行
                cmdObj[cmd]();
            });
        })
    </script>
</head>
<body>
<%--班级管理表格--%>
<table id="classgrade_daragrid"></table>
<%--增删改刷新--%>
<div id="classgrade_linkbuttons">
    <a class="easyui-linkbutton" data-options="iconCls:'icon-add'" plain="true" data-cmd="addClass">增加</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-remove'" plain="true" data-cmd="removeClass">删除</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-edit'" plain="true" data-cmd="editClass">编辑</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-reload'" plain="true" data-cmd="reloadClass">更新</a>
</div>

<div id="classgrade_add_linkbuttons">
    <a class="easyui-linkbutton" data-options="iconCls:'icon-ok'" data-cmd="save">确定</a>
    <a class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" data-cmd="cancel">取消</a>
</div>
<%--新增和编辑的弹窗--%>
<div id="classgrade_add_dialog" style="padding: 10px">
    <form id="classgrade_add_form" method="post">
        <input type="hidden" name="id" id="class_id">
        <table>
            <tr>
                <td>班级名称</td>
                <td><input name="name" class="easyui-textbox"></td>
            </tr>
            <tr>
                <td>学生数量</td>
                <td><input name="stuNumber" class="easyui-textbox"></td>
            </tr>
            <tr>
                <td>班主任</td>
                <td><input id="combobox_employee" class="easyui-combobox" name="employee.id"
                           data-options="valueField:'id',textField:'realName',url:'/employee/listAllEmployeeBySn?sn=CLZMGR'"/></td>
            </tr>
            <tr>
                <td>所在校区</td>
                <td><input id="combobox_campus" class="easyui-combobox" name="campus.id"
                           data-options="valueField:'id',textField:'name',url:'/systemDictionaryItem/listAllItemBySn?sn=campus'"/></td>
            </tr>
            <tr>
                <td>所属学院</td>
                <td><input id="combobox_college" class="easyui-combobox" name="college.id"
                           data-options="valueField:'id',textField:'name',url:'/systemDictionaryItem/listAllItemBySn?sn=college'"/></td>
            </tr>
               <tr>
                <td>开班时间</td>
                <td><input  name="classBeginTime"   class= "easyui-datebox" required ="required"> </input> </td>
            </tr>
                <tr>
                <td>结课时间</td>
                <td><input  name="classEndTime"   class= "easyui-datebox" required ="required"> </input> </td>
            </tr>
            <tr>
                <td>使用教室</td>
                <td><input id="combobox_classroom" class="easyui-combobox" name="classroom.id"
                           data-options="valueField:'id',textField:'name',url:'/classroom/listAll'"/></td>
            </tr>
        </table>
    </form>
</div>
</body>
</html>
